package display;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.HashMap;

import service.VentaManager;
import domain.Venta;

//import service.CDManager;
//import domain.Grupo;

@Controller
@RequestMapping("/venta")
public class VentaController {
 
 @Autowired
 private VentaManager ventaManager;
 
 //@Autowired
 //private CDMnager CDManager;
 
 @RequestMapping(value="/listado", method = RequestMethod.GET)
 public ModelAndView listado() {

   Map<String, Object> myModel = new HashMap<String, Object>();
   myModel.put("ventas", this.ventaManager.getAll());
   return new ModelAndView("listaVentas", "model", myModel);

 }
 
 @RequestMapping(value="/detalleVenta/{pk_venta}", method = RequestMethod.GET)
 public ModelAndView detalleVenta(@PathVariable("pk_venta") String id) {

   ModelAndView modelAndView = new ModelAndView();
   Venta element = this.ventaManager.getById(id);

   if (element == null){
     System.out.println("NULO");
	}else{ 
    modelAndView = new ModelAndView("detalleVenta");
    modelAndView.addObject("venta", element);
		//Map<String, Object> myModel = new HashMap<String, Object>();
    //myModel.put("CDs", this.CDManager.getAllByBandaID(id));
		//modelAndView.addObject("model", myModel);
   }
    return modelAndView;	 
 }
 

 @RequestMapping(value="/actualizarVenta/{pk_venta}", params = { "pk_venta","fecha","metodoPago","fk_dependiente","fk_cliente" }, method = RequestMethod.GET)
 public String actualizarVenta(
     @PathVariable("pk_venta") String pk_venta, 
     @RequestParam("fecha") String fecha,
     @RequestParam("metodoPago") String metodoPago,
     @RequestParam("fk_dependiente") String fk_dependiente,
     @RequestParam("fk_cliente") String fk_cliente
     
  ) {
   Venta element = this.ventaManager.getById(pk_venta);
   element.setFecha(fecha);
   element.setMetodoPago(metodoPago);
   element.setFk_dependiente(fk_dependiente);
   element.setFk_cliente(fk_cliente);   
   return "forward:/venta/listado";
 }
 
 @RequestMapping(value="/agregarVenta", method = RequestMethod.GET)
 public ModelAndView aregar() {
 	return new ModelAndView("agregarVenta");
 }
 

 @RequestMapping(value="/insertarVenta", params = { "fecha","metodoPago","fk_dependiente","fk_cliente"}, method = RequestMethod.GET)
 public String actualizar(
     @RequestParam("fecha") String fecha,
     @RequestParam("metodoPago") String metodoPago,
     @RequestParam("fk_dependiente") String fk_dependiente,
     @RequestParam("fk_cliente") String fk_cliente
  ) {



   Venta element = new Venta();
   element.setFecha(fecha);
   element.setMetodoPago(metodoPago);
   element.setFk_dependiente(fk_dependiente);
   element.setFk_cliente(fk_cliente);
   this.ventaManager.insert(element);
   return "forward:/venta/listado";
 }
 
  @RequestMapping(value="/eliminarVenta/{pk_venta}", method = RequestMethod.GET)
 public String eliminar(@PathVariable("pk_venta") String id) {
   if (id != null){
	   this.ventaManager.delete(id);
   }
   return "forward:/venta/listado";
 } 

 public void setVentaManager(VentaManager ventaManager) {
   this.ventaManager = ventaManager;
 } 
}